package org.mybatis.generator;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

/**
 * 生成mpper
 * @author Administrator
 *
 */
public class MybatisCreateDaoMapper {
    
    public static void main(String[] args) {
        execute();
    }
    
    public static void execute() {
        System.out.println("========== 开始运行MybatisGenerator ==========");
        try {
            String path = Class.class.getClass().getResource("/").getPath();
            String projectBasePath = findProjectBasePath(path);
            System.out.println(projectBasePath);
            
            File dir = new File(projectBasePath+"/src/main/resources/mapper");
            deleteDir(dir);
            
            String generatorConfigFilePath = projectBasePath + "/src/main/resources/generator/create_dao_mapper.xml";//生成mapper.xml和dao
            if (generatorConfigFilePath.startsWith("/")) {
                generatorConfigFilePath = generatorConfigFilePath.substring(1);
            }
            System.out.println(generatorConfigFilePath);
            System.out.println("start generator ...");
            List<String> warnings = new ArrayList<String>();
            boolean overwrite = true;
            File configFile = new File(generatorConfigFilePath);
            if (configFile.exists()) {
                ConfigurationParser cp = new ConfigurationParser(warnings);
                Configuration config = cp.parseConfiguration(configFile);
                DefaultShellCallback callback = new DefaultShellCallback(overwrite);
                MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
                myBatisGenerator.generate(null);
                System.out.println("end generator!");
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (XMLParserException e) {
            e.printStackTrace();
        }
        catch (InvalidConfigurationException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("========== 结束运行MybatisGenerator ==========");
    }
    
    // 递归删除非空文件夹
    public static void deleteDir(File dir) {
        if (dir.isDirectory()) {
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                deleteDir(files[i]);
            }
        }
        dir.delete();
    }
    
    
    public static String findProjectName(String path) {
        path = path.substring(0, path.length() - 16);
        String projectName = path.substring(path.lastIndexOf("/") + 1, path.length());
        return projectName;
    }
    
    private static String findProjectBasePath(String path) {
        path = path.substring(0, path.length() - 16);
        return path;
    }
}
